package org.hhgzs.modules.erp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hhgzs.modules.erp.constants.BusinessConstants;
import org.hhgzs.modules.erp.entity.Depot;
import org.hhgzs.modules.erp.entity.Person;
import org.hhgzs.modules.erp.mapper.DepotMapper;
import org.hhgzs.modules.erp.service.DepotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hhgzs.modules.erp.service.SystemConfigService;
import org.jeecg.common.system.util.JwtUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DepotServiceImpl extends ServiceImpl<DepotMapper, Depot> implements DepotService {
    private final SystemConfigService systemConfigService;
    private final DepotMapper depotMapper;

    @Override
    public List<Depot> findUserDepot()throws Exception{

        QueryWrapper<Depot> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Depot::getType, 0);
        queryWrapper.lambda().ne(Depot::getDeleteFlag, BusinessConstants.DELETE_FLAG_DELETED);
        queryWrapper.lambda().orderBy(true, true, Depot::getId);
        List<Depot> list = list(queryWrapper);
        return list;

    }


    @Override
    public JSONArray findDepotByCurrentUser() throws Exception {
        JSONArray arr = new JSONArray();
        String type = "UserDepot";
        String userId = JwtUtil.getUserId();
        List<Depot> dataList = findUserDepot();
        //开始拼接json数据
        if (null != dataList) {
            boolean depotFlag = systemConfigService.getDepotFlag();
            for (Depot depot : dataList) {
                JSONObject item = new JSONObject();
                //勾选判断1
                Boolean flag = false;
                try {
                    //flag = userBusinessService.checkIsUserBusinessExist(type, userId.toString(), "[" + depot.getId().toString() + "]");
                } catch (DataAccessException e) {
                    log.error(">>>>>>>>>>>>>>>>>查询用户对应的仓库：类型" + type + " KeyId为： " + userId + " 存在异常！");
                }
                if (!depotFlag || flag) {
                    item.put("id", depot.getId());
                    item.put("depotName", depot.getName());
                    item.put("isDefault", depot.getIsDefault());
                    arr.add(item);
                }
            }
        }
        return arr;
    }
    /**
     * 当前用户有权限使用的仓库列表的id，用逗号隔开
     * @return
     * @throws Exception
     */
    public String findDepotStrByCurrentUser() throws Exception {
        JSONArray arr =  findDepotByCurrentUser();
        StringBuffer sb = new StringBuffer();
        for(Object object: arr) {
            JSONObject obj = (JSONObject)object;
            sb.append(obj.getLong("id")).append(",");
        }
        String depotStr = sb.toString();
        if(ObjectUtil.isNotEmpty(depotStr)){
            depotStr = depotStr.substring(0, depotStr.length()-1);
        }
        return depotStr;
    }
    @Override
    public int updateIsDefault(Long depotId) {
        Depot depot = new Depot();
        UpdateWrapper<Depot> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Depot::getId,depotId);
        updateWrapper.lambda().set(Depot::getIsDefault,true);
        this.update(updateWrapper);

        updateWrapper.clear();

        updateWrapper.lambda().ne(Depot::getId,depotId);
        updateWrapper.lambda().set(Depot::getIsDefault,false);
        this.update(updateWrapper);

        return 1;
    }
}
